package saleMarket.service;

import saleMarket.dao.UserDao;
import saleMarket.domain.User;
import saleMarket.domain.authority.Actor;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 徐元坤
 * @version 1.0  12-07-2020
 */
public final class UserService {
    private static final UserService userService = new UserService();
    private final UserDao userDao = UserDao.getInstance();

    public UserService() {
    }

    public static UserService getInstance() {
        return UserService.userService;
    }

    public Collection<User> findAll() throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Collection<User> users = null;
        try {
            users = userDao.findAll(connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return users;
    }

    public User find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        User user = null;
        try {
            user = userDao.find( id,connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close( connection);
        }
        return user;
    }

    public boolean update(User user) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean updated = false;
        try {
            updated = userDao.update(user,connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close( connection);
        }
        return updated;
    }

    public Integer add(User user,Connection connection) throws SQLException {
        connection.setAutoCommit(false);
        Integer added = 0;
        try {
            added = userDao.add(user,connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            System.out.println("user添加成功");
        }
        return added;
    }

    public boolean delete(Integer id,Connection connection) throws SQLException {
        connection.setAutoCommit(false);
        boolean deleted = false;
        deleted = userDao.delete(id,connection);
        return deleted;
    }
    public Actor login(User userToLogin) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Actor actor = null;
        try {
            actor = userDao.login(userToLogin,connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return actor;
    }

    public User logout(User user) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        try {
            user = userDao.findByName(user.getUsername(),connection);
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return user;
    }
}
